<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2021/7/6
 * Time: 2:49 下午
 */

namespace app\admin\model;


use think\Model;

class AdminBaseModel extends Model
{
    protected $updateTime = false;

    protected static function querySort ($params, $default = ['id' => 'desc']): array {
        $order = [];
        if (isset($params['sort']) && !empty($params['sort']) && strpos($params['sort'], '.')) {
            $sort = explode('.', $params['sort']);
            if (!in_array($sort[1], ['asc', 'desc'])) {
                $sort[1] = 'desc';
            }
            $order[$sort[0]] = $sort[1];
        }
        if (empty($order)) {
            $order = $default;
        }
        return $order;
    }

    protected static function queryLimit ($params, $default = 20): int {
        if (!empty($params['limit']) && $params['limit'] > 0 && $params['limit'] <= 100) {
            $limit = intval($params['limit']);
        }
        if (empty($limit) || $limit <= 0) {
            $limit = $default;
        }
        return $limit;
    }
}
